package com.leji.song.mainPage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.view.LoadImage;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.extra.MyApplication;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.model.DS_ShopComment;
import com.leji.song.mainPage.model.DS_ShopCommentDetail;
import com.leji.song.mainPage.model.DS_ShopInfoDetail;
import com.leji.song.mainPage.model.DS_TranslatePrice;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by mac on 2014/6/14.
 */
public class ShopDetailActivity extends BaseActivity{

    private ImageView shopImg;
    private TextView shopName;
    private TextView sellTime;
    private TextView address;
    private TextView translatePrice;

    private ListView commentListView;

    private ImageView callShop;
    private DS_ShopInfoDetail shopDetail;
    private View shopInfoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.shop_detail);
        initView();

        getDetailInfo();

        getShopComment();
    }



    private void initView() {
        setActivityTitle(getIntent().getStringExtra("shopName"));
        setReturnBtn();

        shopImg = (ImageView) findViewById(R.id.shopImg);

        shopName = (TextView) findViewById(R.id.shopName);
        sellTime = (TextView) findViewById(R.id.sellTime);
        address = (TextView) findViewById(R.id.address);
        translatePrice = (TextView) findViewById(R.id.translatePrice);

        commentListView = (ListView) findViewById(R.id.commentListView);
        callShop = (ImageView) findViewById(R.id.callShop);

        shopInfoView = (View) findViewById(R.id.shopInfoView);



        callShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + shopDetail.getPhone()));
                startActivity(intent);
            }
        });
    }

    private void getDetailInfo() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShopDetail.params.shopsId, getIntent().getStringExtra("shopID"));

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShopDetail.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,ShopDetailActivity.this);
                if (obj != null) {

                    try {

                        Gson gson = new Gson();
                        shopDetail = JsonUtil.fromJson(gson.toJson(obj), DS_ShopInfoDetail.class);

                        LoadImage.display(shopImg, shopDetail.getImgUrl());
                        shopName.setText(shopDetail.getName());
                        setActivityTitle(shopDetail.getName());

                        sellTime.setText("营业时间："+
                                shopDetail.getStarttime() + "-" + shopDetail.getEndtime());

                        address.setText(shopDetail.getAddress());

                        shopInfoView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(ShopDetailActivity.this, MapActivity.class);
                                intent.putExtra("x", Double.valueOf(shopDetail.getX()));
                                intent.putExtra("y", Double.valueOf(shopDetail.getY()));
                                startActivity(intent);
                            }
                        });


                        StringBuffer translateStr = new StringBuffer();
                        Object obj2 = shopDetail.getPricePackage();
                        List<DS_TranslatePrice> tPriceList = new ArrayList<DS_TranslatePrice>();
                        if (obj2 != null) {

                            Gson gson2 = new Gson();
                            Type listType = new TypeToken<List<DS_TranslatePrice>>() {
                            }.getType();

                            tPriceList = gson2.fromJson(gson.toJson(obj2), listType);


                        }

                        if((tPriceList!=null) && (tPriceList.size()>0)){
                            for(DS_TranslatePrice price: tPriceList){

                                translateStr.append(price.getName()+"\n");

                            }
                            translatePrice.setText(translateStr.toString());
                        }

                    }catch (Exception e){
                       e.printStackTrace();
                    }



                }
            }
        });

    }

    private void getShopComment() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetShopComment.params.shopsId, getIntent().getStringExtra("shopID"));

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetShopComment.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o, ShopDetailActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();

                    DS_ShopComment shopComment = JsonUtil.fromJson(gson.toJson(obj), DS_ShopComment.class);

                    try {

                        Object commentObj = shopComment.getDatas();
                        if(commentObj!= null){
                            Type listType = new TypeToken<List<DS_ShopCommentDetail>>() {
                            }.getType();
                            List<DS_ShopCommentDetail> commnetList = gson.fromJson(gson.toJson(commentObj), listType);

                            if((commnetList!= null) &&(commnetList.size()>0)){

                                List<HashMap<String, String>> mapList =  new ArrayList<HashMap<String, String>>();
                                for(DS_ShopCommentDetail comment: commnetList){
                                    HashMap<String, String> map = new HashMap<String, String>();
                                    map.put("name", comment.getUsername());
                                    map.put("comment", comment.getComment());
                                    map.put("time", comment.getTime());

                                    mapList.add(map);
                                }


                                commentListView.setAdapter(new SimpleAdapter(ShopDetailActivity.this, mapList,
                                        R.layout.shop_comment_adapter, new String[]{"name", "comment", "time"},
                                        new int[]{R.id.name, R.id.comment, R.id.time}));

                            }


                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }


                }
            }
        });

    }

}

